<template>
  <el-dialog
    title="上传拱顶下沉数据"
    :visible.sync="dialogUploadVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form
      :inline="true"
      ref="form"
      class="demo-form-inline"
      :model="currentRow"
      label-width="100px"
    >
      <el-form-item label="Excel文件">
        <el-upload
          name="file"
          class="upload-demo"
          action="string"
          :http-request="beforeAvatarUploadThree"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :limit="1"
          :on-success="handlesuccess"
          :on-error="handleerror"
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="upload">确 定</el-button>
      <el-button @click="dialogUploadVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import api from '../../../api'
import qs from 'qs'

export default {
  data() {
    return {
      wId: '',
      dialogUploadVisible: false,
      currentRow: {},
      fileList: []
    }
  },
  props: {
    flag: {
      type: Boolean,
      default: false
    }
  },
  mounted() {
    this.$bus.$on('onAddMonitorInfoUploadShow', (flag) => {
      this.dialogUploadVisible = flag
    })

    this.$bus.$on('onAddMonitorInfoWid', (flag) => {
      this.wId = flag
    })
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 上传图片前的过滤
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1000
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 200MB!')
      }
      return isLt2M
    },
    handlesuccess(response, file, fileList) {
      if (response.code == 2001) {
        this.form.resources = response.data
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    handlePreview(file) {
      console.log(file)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handleerror(err, file, fileList) {
      console.log(err)
    },
    beforeAvatarUploadThree(file) {
      if (this.wId == '') {
        this.$message({
          type: 'fail',
          message: '请选择洞幅'
        })
        return
      }
      let params = new FormData()
      params.append('file', file.file)
      params.append('wId', this.wId)
      api
        .monitorMeasurementUpload(params)
        .then((res) => {
          if (res.data.code == 2001) {
            this.$message({
              type: 'success',
              message: res.data.message
            })
          } else {
            this.$message({
              type: 'fail',
              message: res.data.message
            })
          }
        })
        .catch((err) => {})
    },
    upload(){
      
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  width: 50px !important;
  height: 50px !important;
  font-size: 28px;
  line-height: 50px !important;
  color: #8c939d;
  text-align: center;
}
.avatar {
  display: block;
  width: 50px !important;
  height: 50px !important;
}
.imgbeforepad {
  padding-top: 0px !important;
}
.avatar-uploader .el-icon-plus {
  margin: 15px !important;
}
</style>